import {Injectable} from  '@angular/core';
import {Http,Headers,Response,URLSearchParams,RequestOptions,Request,RequestMethod} from '@angular/http';
//import { Geolocation } from 'ionic-native';
import {Observable} from  'rxjs/Observable';
import 'rxjs/Rx';

@Injectable()
export class AppService {
	 private hostURL:string = "http://117.78.36.121:10850/";
	 //private hostURL:string = "http://192.168.0.109:10850/";
	 //private hostURL:string = "http://15b3624s27.51mypc.cn/";
	// 构造函数
	constructor(
	    private http: Http
	) {

	}
	// 获取抽屉菜单
	getIndexSlide(areaId) {
	    return this.http.get(this.hostURL+"index_slide_img?areaId="+areaId).map(
	        res => res.json().data
	    ).catch(this.handleError);
	}
	getPositiong(latitude,longitude) {
	    return this.http.get("http://api.map.baidu.com/geocoder/v2/?ak=5qalsK4CnNfUprePCL8g2qiu0nNuZZM4&location="+latitude+","+longitude+"&output=json&pois=0").map(
	        res => res.json().result
	    ).catch(this.handleError);
	}
	getAreaAll() {
	    return this.http.get(this.hostURL+"areaAll").map(
	        res => res.json().data
	    ).catch(this.handleError);
	}
	getDpList(hospitalId){
		return this.http.get(this.hostURL+"getDpList?hospitalId="+hospitalId).map(
	        res => res.json().data
	    ).catch(this.handleError);
	}
	get_advice_doc(params){
		let searchParams: URLSearchParams = new URLSearchParams();
		for (let param in params) {
            searchParams.set(param, params[param]);
        }
	    return this.http.get(this.hostURL+"get_advice_doc",{search: searchParams}).map(
	        res => res.json().data
	    ).catch(this.handleError);
	}
	getApiData(url,params){
		let searchParams: URLSearchParams = new URLSearchParams();
		for (let param in params) {
            searchParams.set(param, params[param]);
        }
	    return this.http.get(this.hostURL+url,{search: searchParams}).map(
	        res => res.json().data
	    ).catch(this.handleError);
	}
    login(params){
    	let searchParams: URLSearchParams = new URLSearchParams();
		for (let param in params) {
            searchParams.set(param, params[param]);
        }
        return this.http.post(this.hostURL+'account/login', searchParams).map(
        	res => res.json().data
        	).catch(this.handleError);
    }
    postRopRequest(url,params,sessionId){
        let searchParams: URLSearchParams = new URLSearchParams();
		for (let param in params) {
            searchParams.set(param, params[param]);
        }
        let headers=new Headers({'sessionId':sessionId});
        let options = new RequestOptions({headers: headers});
        console.log(headers);
     //    return this.http.post(this.hostURL+url,{search: searchParams},options).map(
	    //     res => res.json().data
	    // ).catch(this.handleError);
	    return this.http.request(new Request({
	    	url:this.hostURL+url,
	    	method:RequestMethod.Post,
	    	headers:headers,
	    	search:searchParams
	    })).map(
	        res => res.json().data
	    ).catch(this.handleError);
    }
    
	getOrderDpDoc(depId,hospitalId,appointmentDate) {
	    return this.http.get(this.hostURL+"getOrderDpDoc?depId="+depId+"&hospitalId="+hospitalId+"&appointmentDate="+appointmentDate).map(
	        res => res.json().data
	    ).catch(this.handleError);
	}
	// getCurrentPosition(){
	// 	return new Promise(resolve=>{
	// 		Geolocation.getCurrentPosition().then((resp) => {
	// 			console.log(resp.coords.latitude+','+resp.coords.longitude);
	// 			return this.getPositiong(resp.coords.latitude,resp.coords.longitude).subscribe(data =>{
	// 				console.log(data);
	// 				//this.positionData=data;
	// 			});
	// 		}).catch(this.handleError)
	// 	})
		
	// }
	// 处理错误


	
	private handleError(error: Response) {
	    console.log(error);
	    return Observable.throw(error.json().error || 'Server Error');
	}
}